{% extends 'admin_base.html' %}
{% block title %}Comments{% endblock %}
{% block main %}
<div class="row">
    <h3>Comments</h3><br>

    <div class="row">
        <div class="col-md-8">
            <ul class="nav nav-pills">
              <li role="presentation" {% if status=='pending' %}class="active"{% endif %}><a href="{{ url_for('blog_admin.comments') }}">Pending</a></li>
              <li role="presentation" {% if status=='approved' %}class="active"{% endif %}><a href="{{ url_for('blog_admin.comments_approved') }}">Approved</a></li>
              <li role="presentation" {% if status=='spam' %}class="active"{% endif %}><a href="{{ url_for('blog_admin.comments_spam') }}">Spam</a></li>
            </ul>
        </div>
        <div class="col-md-4">
            <form class="form-inline" style="text-align: right;">
              <div class="form-group">
                <!-- <label class="sr-only" for="exampleInputAmount">Amount (in dollars)</label> -->
                <div class="input-group">
                  <div class="input-group-addon"><i class="fa fa-search" aria-hidden="true"></i></div>
                  <input type="text" class="form-control" placeholder="keyword" name="keyword"
                    {% if keyword %} value={{keyword}} {% endif %}
                  >
                  
                </div>
              </div>
              <!-- <button type="submit" class="btn btn-primary">Transfer cash</button> -->
            </form>
        </div>
    </div>

    <br>

    <table class="table table-striped">
        <tr>
            <th style="width: 5%"></th>
            <th style="width: 15%">Author</th>
            <th>Comment</th>
            <th style="width: 15%">Action</th>
        </tr>

        {% for comment in comments.items %}
        <tr>
            <td><input type="checkbox" name="comment_ids" value="{{ comment.id }}"></td>
            <td>
                <p>{{ comment.author }}</p>
                <p>
                    <a href="mailto:{{ comment.email }}" target="_top"  title="{{ comment.email }}"><i class="fa fa-envelope" aria-hidden="true"></i> </a>
                    
                    {% if comment.homepage %}
                    <a href="{{ comment.homepage }}" target="_blank"><i class="fa fa-home" aria-hidden="true"></i></a>
                    {% endif %}
                </p>
            </td>
            <td>
                <div>
                    {{ comment.html_content|safe }}
                </div>
                <!-- <hr> -->
                <br>
                <p>
                    <a href="{{ url_for('main.post_detail', slug=comment.post_slug) }}" target="_blank"><i class="fa fa-search" aria-hidden="true"></i> {{ comment.post_title }}</a> 

                    &nbsp;&nbsp;&nbsp;&nbsp;  

                    <i class="fa fa-clock-o" aria-hidden="true"></i> {{ comment.update_time.strftime('%Y-%m-%d %H:%M:%S') }}
                </p>
            </td>
            <td>
                {% if status!='approved' %}
                <a class="btn btn-success approve" href="{{ url_for('blog_admin.comment_action', pk=comment.id ) }}" role="button">Approve</a>
                {% endif %}
                <a class="btn btn-danger delete" href="{{ url_for('blog_admin.comment_action', pk=comment.id ) }}" role="button">Delete</a>
            </td>
        </tr>
        {% else %}
        <tr><td></td><td colspan="3">No records yet!</td></tr>
        {% endfor %}

    </table>
    {% import '_pagination.html' as pagination %}
    {{ pagination.render_pagination(comments) }}
</div>
{% endblock %}

{% block js %}
<script type="text/javascript">
$('.approve').on('click', function (event) {
    var url = $(this).attr('href');
    url = url + '?ajax=true';
    // bootbox.confirm("Are you sure?", function(result) {
    //   if(result){
    //     $.ajax({
    //         url: url,
    //         type: 'PUT',
    //         success: function(result){
    //             location.reload();
    //         }

    //     })
    //     }
    // });
    $.ajax({
        url: url,
        type: 'PUT',
        success: function(result){
            location.reload();
        }

    });
    return false;
  });

$('.delete').on('click', function (event) {
    var url = $(this).attr('href');
    url = url + '?ajax=true';
    bootbox.confirm("Are you sure?", function(result) {
      if(result){
        $.ajax({
            url: url,
            type: 'DELETE',
            success: function(result){
                location.reload();
            }

        })
        }
    });
    return false;
  });
</script>
{% endblock %}
